package com.chunxuan.langquan.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chunxuan.langquan.R;
import com.chunxuan.langquan.base.Base2Activity;
import com.chunxuan.langquan.base.BaseResult2;
import com.chunxuan.langquan.base.Global;
import com.chunxuan.langquan.dao.bean.ReceiptAddressInfo;
import com.chunxuan.langquan.dao.http.APIRetrofit;
import com.chunxuan.langquan.ui.adapter.AddressListAdapter;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.util.ToastUtils;

import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.functions.Consumer;

public class AddressManageActivity extends Base2Activity {
    private TextView tvAddAddress;
    private RecyclerView rvAddressList;
    private ImageView ivBack;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_address_add;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tvAddAddress = findViewById(R.id.tv_add_address);
        rvAddressList = findViewById(R.id.rv_address_list);
        ivBack=findViewById(R.id.iv_back);
        setOnClickListener(tvAddAddress,ivBack);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getAddressList();
    }

    private void getAddressList() {
        APIRetrofit.getDefault().getAddressList(Global.HEADER, "")
                .compose(RxUtil.<BaseResult2<List<ReceiptAddressInfo>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<ReceiptAddressInfo>>>() {
                    @Override
                    public void accept(BaseResult2<List<ReceiptAddressInfo>> result2) throws Exception {
                        hideProgress();
                        if (result2.isSuccess()) {
                            if (null != result2.getData() && result2.getData().size() > 0) {
                                AddressListAdapter addressListAdapter =
                                        new AddressListAdapter(AddressManageActivity.this,
                                                result2.getData());
                                LinearLayoutManager linearLayoutManager =
                                        new LinearLayoutManager(AddressManageActivity.this);
                                rvAddressList.setLayoutManager(linearLayoutManager);
                                rvAddressList.setAdapter(addressListAdapter);
                            }
                        } else {
                            ToastUtils.showShort(result2.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        ToastUtils.showShort("获取收货地址列表失败");
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_add_address:
                startActivity(AddAddressActivity.class);
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

}
